import { createRouter, createWebHistory } from "vue-router";

const routes = [
	{
		path: "/login",
		name: "Login",
		component: () => import("@/views/LoginView.vue"),
	},
	{
		path: "/",
		name: "Index",
		component: () => import("@/views/IndexView.vue"),
		// 授权
		meta: { requiredAuth: true },
		children: [
			{
				// 重定向
				path: "",
				name: "IndexRedirect",
				redirect: "main",
			},
			{
				path: "main",
				name: "Main",
				component: () => import("@/views/MainView.vue"),
				meta: { requiredAuth: true },

			},
			{
				path: "customer",
				name: "Customer",
				component: () => import("@/views/CustomerView.vue"),
				meta: { requiredAuth: true },

			},
			{
				path: "powerZone",
				name: "PowerZone",
				component: () => import("@/views/PowerZoneView.vue"),
				meta: { requiredAuth: true },
			},
			{
				path: "user",
				name: "User",
				component: () => import("@/views/UserView.vue"),
				meta: { requiredAuth: true },
			},
			{
				path: "powerEquipment",
				name: "PowerEquipment",
				component: () => import("@/views/PowerEquipmentView.vue"),
				meta: { requiredAuth: true },
			},
			{
				path: "customerDemand",
				name: "CustomerDemand",
				component: () => import("@/views/CustomerDemandView.vue"),
				meta: { requiredAuth: true },
			},
			
			{
				path: "prioritySupport",
				name: "PrioritySupport",
				component: () => import("@/views/PrioritySupportView.vue"),
				meta: { 
					requiredAuth: true,
					requiresSubscription: true 
				},
			},
			{
				path: "customerAnalysis",
				name: "CustomerAnalysis",
				component: () => import("@/views/CustomerAnalysisView.vue"),
				meta: { 
					requiredAuth: true,
					requiresSubscription: true,
					title: "客户分析"
				},
			},
			{
				path: "equipmentAnalysis",
				name: "EquipmentAnalysis",
				component: () => import("@/views/EquipmentAnalysisView.vue"),
				meta: { 
					requiredAuth: true,
					requiresSubscription: true,
					title: "设备分析"
				},
			}
		],
	},
];

const router = createRouter({
	history: createWebHistory(),
	routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
	// 如果目标路由需要授权
	if (to.meta.requiredAuth) {
		// 检查是否有token
		const token = localStorage.getItem("token");
		if (token) {
			// 检查是否需要订阅才能访问
			if (to.meta.requiresSubscription) {
				// 安全地获取订阅状态和管理员状态
				let hasSubscription = "0";
				let isAdmin = false;
				try {
					if (typeof localStorage !== 'undefined') {
						hasSubscription = localStorage.getItem("hasSubscription") || "0";
						isAdmin = localStorage.getItem("isAdmin") === "true";
					}
				} catch (error) {
					console.error('检查订阅和管理员状态失败:', error);
				}
				
				// 如果不是管理员且未订阅，重定向到首页
				if (!isAdmin && hasSubscription !== "1") {
					// 显示提示信息
					alert("该功能需要订阅专业版套餐才能使用");
					next({
						path: "/main",
					});
				} else {
					// 已订阅或管理员，允许访问
					next();
				}
			} else {
				// 不需要订阅，直接导航
				next();
			}
		} else {
			// 没有token，重定向到登录页
			next({
				path: "/login",
				query: { redirect: to.fullPath },
			});
		}
	} else {
		// 目标路由不需要授权，直接导航
		next();
	}
});

export default router;
